package assignment.ton.gui;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JScrollPane;
import javax.swing.JEditorPane;

import assignment.ton.gui.interfaces.EditingInterface;
import assignment.ton.gui.listener.CodeEditorFrameActionListener;

/**
 * The code editing frame
 * 
 * @author Shaun Close (shc10)
 */

public class CodeEditorFrame extends JFrame {

	private static final long serialVersionUID = -1044541999474220602L;
	private JEditorPane codeEditorPane;
	private CodeEditorFrameActionListener codeListener;
	private MainFrame mainFrame;
	private JMenu file;
	private JMenuItem save, close;
	private EditingInterface editing;
	/**
	 * Tims constructor
	 * 
	 * @param mF the main frame
	 * @param ei the editing interface eg, the edit mthod or classframe
	 * @param lineNumberPane
	 */
	public CodeEditorFrame(MainFrame mF, EditingInterface ei,
			JEditorPane lineNumberPane) {
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		final CodeEditorFrame iAm = this;
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				iAm.dispose();
				codeListener.cancel();
			}
		});
		editing = ei;
		this.setIconImage(new ImageIcon(getClass().getResource("ton_icon.gif"))
				.getImage());
		mainFrame = mF;
		codeListener = new CodeEditorFrameActionListener(this,
				mainFrame);
		this.setTitle("TON Code Editor");
		this.setResizable(true);
		this.setLayout(new GridBagLayout());
		this.setLocationRelativeTo(mainFrame);
		this.setLayout(new BorderLayout());
		// editorPane
		codeEditorPane = new JEditorPane();
		JScrollPane scroll = new JScrollPane(codeEditorPane);
		this.add(scroll, BorderLayout.CENTER);
		this.setSize(1000, 800);
		//section for menu
		JMenuBar menuB = new JMenuBar();
		file = new JMenu("File");
		save = new JMenuItem("Save");
		file.add(save);
		save.addActionListener(codeListener);
		close = new JMenuItem("Close");
		close.addActionListener(codeListener);
		file.add(close);
		menuB.add(file);
		this.add(menuB, BorderLayout.NORTH);
		//end menu
		ArrayList<String> codeStrings;
		codeStrings = ei.getExtraCode();
		String output = "";
		if (codeStrings != null) {
			for (String s : codeStrings) {
				output = output + System.getProperty("line.separator") + s;
			}
			codeEditorPane.setText(output);
		}
		
	}

	public void cancel() {
		mainFrame.setAction(mainFrame.getPreviousAction());
		this.dispose();
	}
	/**
	 * Method to save the text area to the class
	 */
	public void save(){
		String[] input = codeEditorPane.getText().split(System.getProperty("line.separator"));
		ArrayList<String> out = new ArrayList<String>();
		for(int i = 0; i<input.length; i++){
			if(input[i].equals("")==false){
				out.add(input[i]);
			}
		}
		editing.setExtraCode(out);
	}
}